package tech.horgee.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import tech.horgee.common.IWrapper;
import tech.horgee.enums.ApplyStatusEnum;
import tech.horgee.enums.AbsenceTypeEnum;
import tech.horgee.group.UpdateGroup;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * 请假表
 *
 * @author haojun
 * @since 2023-12-13
 */
@Getter
@Setter
@TableName("tab_absence")
public class Absence implements Serializable, IWrapper<Absence> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @NotNull(groups = UpdateGroup.class)
    @TableId(value = "absence_id", type = IdType.AUTO)
    private Long absenceId;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 请假类型
     */
    private AbsenceTypeEnum type;

    /**
     * 说明
     */
    private String remark;

    /**
     * 状态
     */
    private ApplyStatusEnum status;

    /**
     * 乐观锁
     */
    @NotNull(groups = UpdateGroup.class)
    @Version
    private Long version;

    /**
     * 逻辑删除
     */
    @TableLogic
    private Boolean deleted;

    /**
     * 开始时间
     */
    private LocalDate startTime;

    /**
     * 结束时间
     */
    private LocalDate endTime;

    /**
     * 创建时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private LocalDateTime updateTime;

    @TableField(exist = false)
    private String nickname;

    public String getTypeLabel() {
        return Optional.ofNullable(type).orElse(AbsenceTypeEnum.A2).label;
    }

    public String getStatusLabel() {
        return Optional.ofNullable(status).orElse(ApplyStatusEnum.PROCESSING).label;
    }

    @Override
    public LambdaQueryWrapper<Absence> wrapper() {
        return new LambdaQueryWrapper<Absence>()
                .eq(Objects.nonNull(absenceId), Absence::getAbsenceId, absenceId)
                .like(Objects.nonNull(userId), Absence::getUserId, userId)
                .eq(Objects.nonNull(type), Absence::getType, type)
                .like(Objects.nonNull(remark), Absence::getRemark, remark)
                .eq(Objects.nonNull(status), Absence::getStatus, status)
                .eq(Objects.nonNull(version), Absence::getVersion, version)
                .eq(Objects.nonNull(deleted), Absence::getDeleted, deleted)
                .eq(Objects.nonNull(startTime), Absence::getStartTime, startTime)
                .eq(Objects.nonNull(endTime), Absence::getEndTime, endTime)
                .eq(Objects.nonNull(createTime), Absence::getCreateTime, createTime)
                .eq(Objects.nonNull(updateTime), Absence::getUpdateTime, updateTime)
                ;
    }
}
